package org.hbgl.geoentity.db.service;

import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.hbgl.geoentity.db.dao.LogExamineMapper;
import org.hbgl.geoentity.db.dao.LogLoginMapper;
import org.hbgl.geoentity.db.dao.LogOperationMapper;
import org.hbgl.geoentity.db.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class LogService {
    @Resource
    private LogOperationMapper logOperationMapper;
    @Resource
    private LogLoginMapper logLoginMapper;
    @Resource
    private LogExamineMapper logExamineMapper;

    public void add(LogOperation log) {
        log.setOperationTime(LocalDateTime.now());
        logOperationMapper.insertSelective(log);
    }

    public void add(LogExamine log) {
        log.setOperationTime(LocalDateTime.now());
        logExamineMapper.insertSelective(log);
    }

    public List<LogOperation> queryLogOperationSelective(String keywords, String start, String end, Integer page, Integer limit, String sort, String order) {
        LogOperationExample example = new LogOperationExample();
        LogOperationExample.Criteria criteria1 = example.or();
        LogOperationExample.Criteria criteria2 = example.or();

        if (StringUtils.isNotBlank(keywords)) {
            criteria1.andUsernameLike("%" + keywords + "%");
            criteria2.andRealnameLike("%" + keywords + "%");
        }
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        if (StringUtils.isNotBlank(start)) {
            start += " 00:00:00";
            criteria1.andOperationTimeGreaterThanOrEqualTo(LocalDateTime.parse(start, dtf));
            criteria2.andOperationTimeGreaterThanOrEqualTo(LocalDateTime.parse(start, dtf));
        }
        if (StringUtils.isNotBlank(end)) {
            end += " 23:59:59";
            criteria1.andOperationTimeLessThanOrEqualTo(LocalDateTime.parse(end, dtf));
            criteria2.andOperationTimeLessThanOrEqualTo(LocalDateTime.parse(end, dtf));
        }

        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return logOperationMapper.selectByExample(example);
    }
    public List<LogExamine> queryLogExamineSelective(Integer examState, String keywords, String start, String end, Integer page, Integer limit, String sort, String order) {
        LogExamineExample example = new LogExamineExample();
        LogExamineExample.Criteria criteria1 = example.or();
        LogExamineExample.Criteria criteria2 = example.or();

        if (StringUtils.isNotBlank(keywords)) {
            criteria1.andUsernameLike("%" + keywords + "%");
            criteria2.andRealnameLike("%" + keywords + "%");
        }
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        if (StringUtils.isNotBlank(start)) {
            start += " 00:00:00";
            criteria1.andOperationTimeGreaterThanOrEqualTo(LocalDateTime.parse(start, dtf));
            criteria2.andOperationTimeGreaterThanOrEqualTo(LocalDateTime.parse(start, dtf));
        }
        if (StringUtils.isNotBlank(end)) {
            end += " 23:59:59";
            criteria1.andOperationTimeLessThanOrEqualTo(LocalDateTime.parse(end, dtf));
            criteria2.andOperationTimeLessThanOrEqualTo(LocalDateTime.parse(end, dtf));
        }

        if(examState!=-2) {
            criteria1.andExamStateEqualTo(examState);
            criteria2.andExamStateEqualTo(examState);
        }

        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return logExamineMapper.selectByExample(example);
    }


    public void add(LogLogin log) {
        log.setLoginTime(LocalDateTime.now());
        logLoginMapper.insertSelective(log);
    }

    public List<LogLogin> queryLogLoginSelective(String keywords, String start, String end, Integer page, Integer limit, String sort, String order) {
        LogLoginExample example = new LogLoginExample();
        LogLoginExample.Criteria criteria1 = example.or();
        LogLoginExample.Criteria criteria2 = example.or();

        if (StringUtils.isNotBlank(keywords)) {
            criteria1.andUsernameLike("%" + keywords + "%");
            criteria2.andRealnameLike("%" + keywords + "%");
        }
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        if (StringUtils.isNotBlank(start)) {
            start += " 00:00:00";
            criteria1.andLoginTimeGreaterThanOrEqualTo(LocalDateTime.parse(start, dtf));
            criteria2.andLoginTimeGreaterThanOrEqualTo(LocalDateTime.parse(start, dtf));
        }
        if (StringUtils.isNotBlank(end)) {
            end += " 23:59:59";
            criteria1.andLoginTimeLessThanOrEqualTo(LocalDateTime.parse(end, dtf));
            criteria2.andLoginTimeLessThanOrEqualTo(LocalDateTime.parse(end, dtf));
        }

        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return logLoginMapper.selectByExample(example);
    }
}
